<template>
  <div class="pages-article-detail">
    <div
      v-loading="loading"
      :style="{
        'background-image': `url(${articleDetail.coverImg})`,
      }"
      class="cover"
    >
      <div class="content">
        <div class="title">{{ articleDetail.title }}</div>
        <p>
          <span>点赞 {{ articleDetail.praiseNum }}</span>
          ·
          <span>浏览 {{ articleDetail.browseNum }}</span>
          <!-- <span v-if="ipValue">IP {{ ipValue }}</span> -->
        </p>
        <div class="tags">
          <span
            v-for="(item, index) in articleClassify"
            :key="index"
            @click="
              $router.push({
                name: 'classifyDetail',
                params: { id: item.articleClassifyId },
              })
            "
          >
            {{ item.name }}
          </span>
          <!-- <span @click="$router.push('/note/classifyDetail')">JS那些事</span> -->
          <!-- <span @click="$router.push('/note/classifyDetail')">工程化</span> -->
        </div>
      </div>
    </div>

    <main v-loading="loading">
      <div class="article">
        <div class="article-info">
          <div class="left">
            <div v-if="articleDetail.articleType == 2" class="original">
              原创
            </div>
            <span v-if="ipValue">IP {{ ipValue }}</span>
          </div>
          <div class="right">{{ articleDetail.createDate }}</div>
        </div>
        <div class="content">
          <layRender
            v-if="articleDetail.contentType == 2"
            :content="articleDetail.modelEditContent"
          ></layRender>

          <markdown-highlight
            v-if="articleDetail.contentType == 1"
            :value="articleDetail.htmlContent"
          ></markdown-highlight>
        </div>
        <div
          @click="addPraise"
          :class="['like', { active: articleDetail.isPraise == 1 }]"
        >
          <svg-icon icon-class="like-fill"></svg-icon>
          <div>
            支持一下({{
              articleDetail.praiseNum < 10
                ? "0" + articleDetail.praiseNum
                : articleDetail.praiseNum
            }})
          </div>
        </div>
      </div>
      <div class="comment-list">
        <comment :targetId="articleId"></comment>
      </div>
    </main>
  </div>
</template>

<script>
import comment from "@/components/comment/comment";
import layRender from "vue2-lay-render";
import markdownHighlight from "@/components/markdown-highlight/index.vue";
import ColorThief from "colorthief";

export default {
  name: "WorkspaceJsonArticleDetail",
  components: {
    comment,
    layRender,
    markdownHighlight,
  },
  data() {
    return {
      loading: true,
      articleId: "",
      articleDetail: {},
      articleClassify: [],
      bgColor: "",
    };
  },

  mounted() {
    this.articleId = this.$route.params.id;
    this.getArticleDetail();
  },

  computed: {
    ipValue() {
      let territory = this.articleDetail.territory || "";
      return territory.split("-")[1] || "";
    },
  },
  methods: {
    getArticleDetail() {
      this.$http("getArticleDetail", { articleId: this.articleId }).then(
        async (res) => {
          this.articleDetail = res.data;
          this.articleClassify = res.data.articleClassify;
          document.title = this.articleDetail.title;
          this.loading = false;
        }
      );
    },
    // 点赞
    addPraise(item) {
      if (this.articleDetail.isPraise == 1) return;
      this.$http("addPraise", {
        targetId: this.articleId,
        targetType: "1",
      }).then((res) => {
        this.articleDetail.isPraise = 1;
        this.articleDetail.praiseNum += 1;
        this.$notify({
          title: "赠人玫瑰,手留余香",
          message: "你的支持是我前进的动力!",
          type: "success",
        });
      });
    },
    formatDate(d) {
      let date = new Date(d);
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      month = month < 10 ? "0" + month : month;
      let day = date.getDate();
      day = day < 10 ? "0" + day : day;
      return `${year}年${month}月${day}日`;
    },
  },
};
</script>

<style lang="scss" scoped>
.pages-article-detail {
  width: 100%;
  height: 100%;
}

.cover {
  height: 60%;
  position: relative;
  background: #fff;
  // background-image: url("https://dongping-blog.oss-cn-hangzhou.aliyuncs.com/systemImage/48380973b3514d2ca84472bf697088d7.png");
  background-position: center center;
  background-size: cover;
  background-repeat: no-repeat;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  &::before {
    content: "";
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.6);
  }
  .content {
    color: #fff;
    position: relative;
    z-index: 9;
    text-align: center;
    letter-spacing: 1.2px;
    max-width: 800px;

    .title {
      font-size: 30px;
      font-weight: 600;
      padding: 0 20px;
      @include main-text-hidden(2);
    }
    p {
      font-size: 16px;
      opacity: 0.8;
      margin: 10px 0;
      span {
        margin: 0 5px;
      }
    }
    .tags {
      margin-top: 5px;
      display: flex;
      justify-content: center;
      span {
        height: 25px;
        line-height: 25px;
        font-size: 14px;
        opacity: 0.8;
        margin: 0 10px;
        padding: 0px 8px;
        box-shadow: 0 0.5rem 1rem rgba(51, 52, 53, 1);
        border: 1px solid rgba(255, 255, 255, 0.2);
        border-radius: 20px;
        backdrop-filter: blur(80px);
      }
    }
  }
}

main {
  max-width: 800px;
  margin-top: -80px;
  padding: 0 10px 1px;
  margin: -80px auto 0;
  position: relative;
  z-index: 8;
  .article-info {
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #f5f5f5;
    padding: 20px;
    color: #999;
    .left {
      display: flex;
      align-items: center;
      .original {
        margin-right: 10px;
        background: #f5f5f5;
        padding: 2px 4px;
        font-size: 14px;
      }
    }
  }

  .article {
    background: #fff;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.07);
    border-radius: 6px;
    padding-bottom: 1px;
    .content {
      min-height: 60vh;
      padding: 20px;
    }
    .like {
      height: 50px;

      margin: 0 auto 50px;

      text-align: center;
      color: #999;

      .svg-icon {
        margin: auto;
        font-size: 22px;
      }
      div {
        font-size: 14px;
        margin-top: 5px;
      }
    }
    .active {
      color: rgba(46, 188, 60, 1);
      border-color: rgba(46, 188, 60, 1);
    }
  }

  .comment-list {
    background: #ffff;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.07);
    padding: 20px;
    margin: 20px 0 100px;
  }
}

/deep/ .lay-text-type-1 {
  div {
    min-width: 200px !important;
  }
}

@media (max-width: 550px) {
  .content {
    .title {
      font-size: 22px !important;
    }
    p {
      font-size: 14px !important;
    }
  }

  main {
    padding: 0;
  }
  .article {
    .content {
      padding: 20px 5px !important;
    }
  }
}
</style>
